import {action, makeAutoObservable, observable} from "mobx";
import {AxioInstance} from "../api/AxiosHelper";
import {
    GET_COURSE_LIST,
    GET_COURSE_TAG,
    GET_COURSE_TYPE,
    GET_COURSE_USERS,
    GET_EXP_USERS,
    GET_USER_COURSE_LIST
} from "../api/APIURLDefine";

/*课程信息*/
class CourseStore {

    constructor() {
        makeAutoObservable(this)
    }

    //模块
    @observable course = 0;  // 0 所有课程  1 我的课程

    @action SetCourse(value: number) {
        this.course = value;
    }

    //当前选择的课程id
    @observable more: boolean = false;

    @action SetMore(value: boolean) {
        this.more = value;
    }

    //课程类型
    @observable courseType = 0;

    @action SetCourseType(value: number) {
        this.courseType = value;
        console.log(`课程类型id:${value}`);
        this.getData();
    }

    //模块
    @observable mode = 0;

    @action SetMode(value: number) {
        this.mode = value;
        this.getData();
    }

    //课程列表
    @observable courseTag = 0;

    @action SetCourseTag(value: number) {
        this.courseTag = value;
        this.getData();
    }

    @observable data = null;

    @action SetData(value: any) {
        this.data = value;
    }

    @observable studyRecord = null;

    @action SetStudyRecord(value: any) {
        this.studyRecord = value;
    }

    @observable expRecord = null;

    @action SetExpRecord(value: any) {
        this.expRecord = value;
    }

    getData() {
        const path = this.course === 0 ? GET_COURSE_LIST : GET_COURSE_USERS;
        AxioInstance.get(`${path}/${this.page}/${this.pageSize}?tag=${this.courseTag}&type=${this.courseType}&mode=${this.mode}`)
            .then((data: any) => {
                this.SetData(data);
                this.SetTotal(data.total);
                this.SetPage(data.current);
                // this.setState({dataSource: data, total: data.total, mode: mode});
            }).catch((e) => {
            //console.error(e);
        })
    }

    //当前页
    @observable page = 1;

    @action SetPage(value: number) {
        this.page = value;
    }

    @observable pageSize = 12;

    @action SetPageSize(value: number) {
        this.pageSize = value;
    }

    //课程总数
    @observable total = 1;

    @action SetTotal(value: number) {
        this.total = value;
        //console.log(`total:${value}`);
    }


    getTypeAndTag() {
        this.getType();
        this.getTag();
    }

    @observable types = [];

    @action SetTypes(value: any) {
        this.types = value;
    }

    getType() {
        //获取课程类型
        AxioInstance.get(`${GET_COURSE_TYPE}/1/30`).then((data: any) => {
            // console.log(data);
            const list = [{id: '0', typeName: "全部"}];
            var temp = list.concat(data.records);
            this.types = temp;
        }).catch((e) => {
            //console.error(e);
        })
    }

    @observable tags = [];

    @action SetTags(value: any) {
        this.tags = value;
    }

    getTag() {
        //获取课程标签
        AxioInstance.get(`${GET_COURSE_TAG}/1/30?type=1`).then((data: any) => {
            // console.log(data);
            const list = [{id: '0', tag: "全部"}];
            let temp = list.concat(data.records);
            this.tags = temp;
        }).catch((e) => {
            console.error(e);
        })
    }

    //获取学习记录
    getStudyRecord() {
        // /v1/user/courseUsers/{page}/{count}
        AxioInstance.get(`${GET_COURSE_USERS}/1/999`)
            .then((data: any) => {
                this.SetStudyRecord(data.records);
            }).catch((e) => {
            console.error(e);
        })
    }

    // /v1/user/stat/userExp/{page}/{count}
    getExpRecord() {
        AxioInstance.get(`${GET_EXP_USERS}/1/10`)
            .then((res: any) => {
                this.SetExpRecord(res.records);
            }).catch(
            error => console.log(error)
        )
    }

    getMyCourseData() {
        this.SetCourse(1);
        this.SetMode(0);
        this.getTypeAndTag();
        this.getData();
    }

}

export default CourseStore;